package com.fr.report.adhoc;

import com.fr.base.ColumnRow;
import com.fr.base.Inter;
import com.fr.base.StringUtils;
import com.fr.base.core.json.JSONArray;
import com.fr.base.core.json.JSONObject;
import com.fr.data.util.function.DataFunction;
import com.fr.report.CellElement;
import com.fr.report.DefaultCellElement;
import com.fr.report.WorkBook;
import com.fr.report.WorkSheet;
import com.fr.report.cellElement.CellExpandAttr;
import com.fr.report.cellElement.Formula;
import com.fr.report.cellElement.core.DSColumn;
import com.fr.report.cellElement.core.FunctionGrouper;
import com.fr.report.cellElement.core.RecordGrouper;
import com.fr.report.cellElement.core.SummaryGrouper;
import com.fr.report.core.SheetUtils;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class SimpleADHOCReport extends NormalADHOCReport
{
  public static int TYPE = 0;
  private SimpleADHOCColumn[] displayColumns;
  private ADHOCColumn[] groupColumns;
  private ADHOCColumn sortColumn;
  private SimpleADHOCStyle simpleADHOCStyle;
  private List detailColumns;

  public SimpleADHOCStyle getSimpleADHOCStyle()
  {
    return this.simpleADHOCStyle;
  }

  public void setSimpleADHOCStyle(SimpleADHOCStyle paramSimpleADHOCStyle)
  {
    this.simpleADHOCStyle = paramSimpleADHOCStyle;
  }

  public SimpleADHOCColumn[] getDisplayColumns()
  {
    return this.displayColumns;
  }

  public void setDisplayColumns(SimpleADHOCColumn[] paramArrayOfSimpleADHOCColumn)
  {
    this.displayColumns = paramArrayOfSimpleADHOCColumn;
  }

  public ADHOCColumn[] getGroupColumns()
  {
    return this.groupColumns;
  }

  public void setGroupColumns(ADHOCColumn[] paramArrayOfADHOCColumn)
  {
    this.groupColumns = paramArrayOfADHOCColumn;
  }

  public ADHOCColumn getSortColumns()
  {
    return this.sortColumn;
  }

  public void setSortColumns(ADHOCColumn paramADHOCColumn)
  {
    this.sortColumn = paramADHOCColumn;
  }

  private List getDetailColumns()
  {
    if (this.detailColumns == null)
    {
      this.detailColumns = new ArrayList();
      if (this.displayColumns != null)
      {
        int i = 0;
        int j = this.displayColumns.length;
        while (i < j)
        {
          if (isGroupOrSortColumn(this.displayColumns[i]))
            break label70:
          this.detailColumns.add(this.displayColumns[i]);
          label70: ++i;
        }
      }
    }
    return this.detailColumns;
  }

  private boolean isGroupOrSortColumn(SimpleADHOCColumn paramSimpleADHOCColumn)
  {
    if (this.groupColumns != null)
    {
      int i = 0;
      int j = this.groupColumns.length;
      while (i < j)
      {
        if (this.groupColumns[i].getColumnName().equals(paramSimpleADHOCColumn.getColumnName()))
          return true;
        ++i;
      }
    }
    if ((getSummaryColumn() != null) && (getSummaryColumn().getColumnName().equals(paramSimpleADHOCColumn.getColumnName())))
      return true;
    if (this.sortColumn != null)
      return this.sortColumn.getColumnName().equals(paramSimpleADHOCColumn.getColumnName());
    return false;
  }

  public WorkBook createWorkBook()
  {
    WorkBook localWorkBook = new WorkBook();
    localWorkBook.putTableData(getViewName(), getADHOCData());
    WorkSheet localWorkSheet = new WorkSheet();
    localWorkBook.addReport(localWorkSheet);
    ArrayList localArrayList = new ArrayList();
    localArrayList.addAll(titleCells());
    localArrayList.addAll(headerColumns());
    localArrayList.addAll(generateContentColumns());
    localArrayList.addAll(generateOtherColumns());
    Iterator localIterator = localArrayList.iterator();
    while (localIterator.hasNext())
      localWorkSheet.addCellElement((CellElement)localIterator.next());
    SheetUtils.calculateDefaultParent(localWorkSheet);
    if (StringUtils.isNotBlank(getTitle()))
      localWorkSheet.setRowHeight(0, this.simpleADHOCStyle.getRowTitleHeight());
    int i = 0;
    int j = totalWidth();
    while (i < j)
    {
      localWorkSheet.setColumnWidth(i, this.simpleADHOCStyle.getColumnWidth());
      ++i;
    }
    return localWorkBook;
  }

  private int totalWidth()
  {
    return (getDetailColumns().size() + ((this.groupColumns == null) ? 0 : this.groupColumns.length) + ((getSummaryColumn() == null) ? 0 : 1) + ((this.sortColumn == null) ? 0 : 1));
  }

  private List titleCells()
  {
    ArrayList localArrayList = new ArrayList();
    if (StringUtils.isNotEmpty(getTitle()))
    {
      DefaultCellElement localDefaultCellElement1 = new DefaultCellElement(0, 0);
      localDefaultCellElement1.setColumnSpan(totalWidth());
      localDefaultCellElement1.setValue(getTitle());
      localDefaultCellElement1.setStyle(this.simpleADHOCStyle.getTitleStyle());
      localArrayList.add(localDefaultCellElement1);
      if (StringUtils.isNotEmpty(getSubtitle()))
      {
        DefaultCellElement localDefaultCellElement2 = new DefaultCellElement(0, 1);
        localDefaultCellElement2.setColumnSpan(totalWidth());
        localDefaultCellElement2.setValue(getSubtitle());
        localDefaultCellElement2.setStyle(this.simpleADHOCStyle.getSubTitleStyle());
        localArrayList.add(localDefaultCellElement2);
      }
    }
    return localArrayList;
  }

  private int titleHeight()
  {
    int i = 0;
    if (StringUtils.isNotEmpty(getTitle()))
    {
      ++i;
      if (StringUtils.isNotEmpty(getSubtitle()))
        ++i;
    }
    return i;
  }

  private List headerColumns()
  {
    ArrayList localArrayList = new ArrayList();
    int i = 0;
    if (this.groupColumns != null)
    {
      i = this.groupColumns.length;
      for (j = 0; j < i; ++j)
      {
        DefaultCellElement localDefaultCellElement1 = new DefaultCellElement(j, titleHeight());
        localDefaultCellElement1.setValue(this.groupColumns[j].getColumnName());
        localDefaultCellElement1.setStyle(this.simpleADHOCStyle.getReportHeaderStyle());
        localArrayList.add(localDefaultCellElement1);
      }
    }
    int j = getDetailColumns().size();
    for (int k = 0; k < j; ++k)
    {
      DefaultCellElement localDefaultCellElement3 = new DefaultCellElement(i + k, titleHeight());
      localDefaultCellElement3.setValue(((SimpleADHOCColumn)getDetailColumns().get(k)).getColumnName());
      localDefaultCellElement3.setStyle(this.simpleADHOCStyle.getReportHeaderStyle());
      localArrayList.add(localDefaultCellElement3);
    }
    if (getSummaryColumn() != null)
    {
      DefaultCellElement localDefaultCellElement2 = new DefaultCellElement(i + j, titleHeight());
      localDefaultCellElement2.setValue(getSummaryColumn().getColumnName());
      localDefaultCellElement2.setStyle(this.simpleADHOCStyle.getReportHeaderStyle());
      localArrayList.add(localDefaultCellElement2);
    }
    return localArrayList;
  }

  private List generateContentColumns()
  {
    DefaultCellElement localDefaultCellElement1;
    CellExpandAttr localCellExpandAttr;
    Object localObject1;
    Object localObject2;
    Object localObject3;
    Object localObject4;
    ArrayList localArrayList = new ArrayList();
    int i = titleHeight() + 1;
    int j = 0;
    int k = getDetailColumns().size() + ((this.sortColumn == null) ? 0 : 1);
    int l = (k == 0) ? 0 : 1;
    if (this.groupColumns != null)
    {
      j = this.groupColumns.length;
      for (i1 = 0; i1 < j; ++i1)
      {
        DefaultCellElement localDefaultCellElement2 = new DefaultCellElement(i1, i);
        localDefaultCellElement2.setStyle(this.simpleADHOCStyle.getReportCotentStyle());
        localObject1 = new CellExpandAttr();
        ((CellExpandAttr)localObject1).setDirection(0);
        localDefaultCellElement2.setCellExpandAttr((CellExpandAttr)localObject1);
        if (getSummaryColumn() != null)
          localDefaultCellElement2.setRowSpan(j - i1 + l);
        localObject2 = new DSColumn();
        ((DSColumn)localObject2).setColumnName(this.groupColumns[i1].getColumnName());
        ((DSColumn)localObject2).setDSName(getViewName());
        ((DSColumn)localObject2).setOrder(this.groupColumns[i1].getSort());
        if (i1 == 0)
          ((DSColumn)localObject2).setCondition(getCondition());
        localObject3 = new FunctionGrouper();
        ((FunctionGrouper)localObject3).setDivideMode(0);
        ((FunctionGrouper)localObject3).setCustom(false);
        ((DSColumn)localObject2).setGrouper((RecordGrouper)localObject3);
        localDefaultCellElement2.setValue(localObject2);
        localArrayList.add(localDefaultCellElement2);
        if ((getSummaryColumn() != null) && (i1 > 0))
        {
          localObject4 = new DefaultCellElement(i1, i + j - i1 + l);
          ((CellElement)localObject4).setStyle(this.simpleADHOCStyle.getSummaryStyle());
          ((CellElement)localObject4).setValue(Inter.getLocText("Summary"));
          ((CellElement)localObject4).setColumnSpan(j - i1 + k);
          localArrayList.add(localObject4);
        }
      }
    }
    int i1 = 0;
    int i2 = getDetailColumns().size();
    while (i1 < i2)
    {
      localObject1 = new DefaultCellElement(j + i1, i);
      ((CellElement)localObject1).setStyle(this.simpleADHOCStyle.getReportCotentStyle());
      localObject2 = new CellExpandAttr();
      ((CellExpandAttr)localObject2).setDirection(0);
      ((CellElement)localObject1).setCellExpandAttr((CellExpandAttr)localObject2);
      localObject3 = new DSColumn();
      ((DSColumn)localObject3).setColumnName(((SimpleADHOCColumn)getDetailColumns().get(i1)).getColumnName());
      ((DSColumn)localObject3).setDSName(getViewName());
      if ((i1 == 0) && (this.groupColumns == null))
        ((DSColumn)localObject3).setCondition(getCondition());
      localObject4 = new FunctionGrouper();
      ((FunctionGrouper)localObject4).setDivideMode(1);
      ((FunctionGrouper)localObject4).setCustom(false);
      ((DSColumn)localObject3).setGrouper((RecordGrouper)localObject4);
      ((CellElement)localObject1).setValue(localObject3);
      localArrayList.add(localObject1);
      ++i1;
    }
    if (this.sortColumn != null)
    {
      localDefaultCellElement1 = new DefaultCellElement(j + getDetailColumns().size(), i);
      localDefaultCellElement1.setStyle(this.simpleADHOCStyle.getReportCotentStyle());
      localCellExpandAttr = new CellExpandAttr();
      localCellExpandAttr.setDirection(0);
      localDefaultCellElement1.setCellExpandAttr(localCellExpandAttr);
      localObject1 = new DSColumn();
      ((DSColumn)localObject1).setColumnName(this.sortColumn.getColumnName());
      ((DSColumn)localObject1).setDSName(getViewName());
      ((DSColumn)localObject1).setOrder(this.sortColumn.getSort());
      if ((this.groupColumns == null) && (getDetailColumns().size() == 0))
        ((DSColumn)localObject1).setCondition(getCondition());
      localObject2 = new FunctionGrouper();
      ((FunctionGrouper)localObject2).setDivideMode(1);
      ((FunctionGrouper)localObject2).setCustom(false);
      ((DSColumn)localObject1).setGrouper((RecordGrouper)localObject2);
      localDefaultCellElement1.setValue(localObject1);
      localArrayList.add(localDefaultCellElement1);
    }
    if (getSummaryColumn() != null)
    {
      localDefaultCellElement1 = new DefaultCellElement(j + k, i);
      localDefaultCellElement1.setStyle(this.simpleADHOCStyle.getReportCotentStyle());
      localCellExpandAttr = new CellExpandAttr();
      localCellExpandAttr.setDirection(2);
      localDefaultCellElement1.setCellExpandAttr(localCellExpandAttr);
      localObject1 = new DSColumn();
      ((DSColumn)localObject1).setColumnName(getSummaryColumn().getColumnName());
      ((DSColumn)localObject1).setDSName(getViewName());
      if ((this.groupColumns == null) && (getDetailColumns().size() == 0) && (this.sortColumn == null))
        ((DSColumn)localObject1).setCondition(getCondition());
      localObject2 = new SummaryGrouper();
      ((SummaryGrouper)localObject2).setFunction(getSummaryColumn().getFunction());
      ((DSColumn)localObject1).setGrouper((RecordGrouper)localObject2);
      localDefaultCellElement1.setValue(localObject1);
      localArrayList.add(localDefaultCellElement1);
    }
    return ((List)(List)(List)(List)localArrayList);
  }

  private List generateOtherColumns()
  {
    int i = getDetailColumns().size() + ((this.sortColumn == null) ? 0 : 1);
    int j = (i == 0) ? 0 : 1;
    ArrayList localArrayList = new ArrayList();
    if (getSummaryColumn() != null)
    {
      DefaultCellElement localDefaultCellElement1 = new DefaultCellElement(0, titleHeight() + this.groupColumns.length + 1 + j);
      localDefaultCellElement1.setValue(Inter.getLocText("Summary"));
      localDefaultCellElement1.setColumnSpan(totalWidth() - 1);
      localDefaultCellElement1.setStyle(this.simpleADHOCStyle.getSummaryStyle());
      localArrayList.add(localDefaultCellElement1);
      if (i > 0)
      {
        localObject = new DefaultCellElement(this.groupColumns.length, titleHeight() + 2);
        ((CellElement)localObject).setValue(Inter.getLocText("Summary"));
        ((CellElement)localObject).setColumnSpan(totalWidth() - 1 - this.groupColumns.length);
        ((CellElement)localObject).setStyle(this.simpleADHOCStyle.getSummaryStyle());
        localArrayList.add(localObject);
      }
      Object localObject = getSummaryColumn().getFunction().getFormula() + "(" + getSummaryCell() + ")";
      int k = 0;
      int l = ((this.groupColumns == null) ? 0 : this.groupColumns.length) + j;
      while (k < l)
      {
        DefaultCellElement localDefaultCellElement2 = new DefaultCellElement(summaryCellPosition()[0], summaryCellPosition()[1] + k + 1);
        localDefaultCellElement2.setValue(new Formula((String)localObject));
        localDefaultCellElement2.setStyle(this.simpleADHOCStyle.getSummaryStyle());
        localArrayList.add(localDefaultCellElement2);
        ++k;
      }
    }
    return ((List)localArrayList);
  }

  private String getSummaryCell()
  {
    return ColumnRow.valueOf(summaryCellPosition()[0], summaryCellPosition()[1]).toString();
  }

  private int[] summaryCellPosition()
  {
    return new int[] { totalWidth() - ((getSummaryColumn() == null) ? 0 : 1), titleHeight() + 1 };
  }

  public JSONObject createJSON()
    throws Exception
  {
    return null;
  }

  public void parseJSON(JSONObject paramJSONObject)
    throws Exception
  {
    Object localObject1;
    int i;
    Object localObject2;
    int j;
    Object localObject3;
    super.parseJSON(paramJSONObject);
    if (paramJSONObject.has("displaycolumns"))
    {
      localObject1 = paramJSONObject.getJSONArray("displaycolumns");
      i = ((JSONArray)localObject1).length();
      localObject2 = new SimpleADHOCColumn[i];
      for (j = 0; j < i; ++j)
      {
        localObject3 = new SimpleADHOCColumn();
        ((SimpleADHOCColumn)localObject3).parseJSON(((JSONArray)localObject1).getJSONObject(j));
        localObject2[j] = localObject3;
      }
      setDisplayColumns(localObject2);
    }
    if (paramJSONObject.has("groupcolumns"))
    {
      localObject1 = paramJSONObject.getJSONArray("groupcolumns");
      i = ((JSONArray)localObject1).length();
      localObject2 = new ADHOCColumn[i];
      for (j = 0; j < i; ++j)
      {
        localObject3 = new ADHOCColumn();
        ((ADHOCColumn)localObject3).parseJSON(((JSONArray)localObject1).getJSONObject(j));
        localObject2[j] = localObject3;
      }
      setGroupColumns(localObject2);
    }
    if (paramJSONObject.has("sortcolumn"))
    {
      localObject1 = new ADHOCColumn();
      ((ADHOCColumn)localObject1).parseJSON(paramJSONObject.getJSONObject("sortcolumn"));
      setSortColumns((ADHOCColumn)localObject1);
    }
    if (paramJSONObject.has("simplestyle"))
      setSimpleADHOCStyle(ADHOCReportStyleUtils.getSimpleStyle(paramJSONObject.getInt("simplestyle")));
  }
}